/*************************************************************************  
 *   This file is part of UBBMPSREADER.
 *
 *   UBBMPSREADER is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   UBBMPSREADER is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with UBBMPSREADER.  If not, see <http://www.gnu.org/licenses/>. 
 *************************************************************************/

package cs.ubbcluj.ro.mps.states;

import java.io.IOException;
import java.io.PushbackInputStream;

import cs.ubbcluj.ro.mps.reader.MpsInputStream;
import cs.ubbcluj.ro.mps.states.interfaces.State;
import cs.ubbcluj.ro.mps.states.utils.CharConstants;

/**
 * Represents a parsing state
 * 
 * @author avadas
 * @version 1.0
 */
public strictfp abstract class AbstractState implements State {

    /** The error state of this state */
    protected final State errorState;

    /** The next state of this state */
    protected final State nextState;

    /**
     * Constructor
     */
    public AbstractState(final State nextState, final State errorState) {
	this.errorState = errorState;
	this.nextState = nextState;
    }

    /**
     * 
     * @param input
     *            an input stream see {@link MpsInputStream}
     * @param nameBuffer
     *            a stringbuffer to fill with the data see {@link StringBuffer}
     * @throws IOException
     *             see {@link IOException}
     */
    protected void consumeData(final MpsInputStream input,
	    final StringBuffer nameBuffer) throws IOException {
	int in = input.read();
	while ((in != 32) && (in != CharConstants.LF)
		&& (in != CharConstants.CR)) {
	    nameBuffer.append((char) in);
	    in = input.read();
	}
	input.unread(in);
    }

    /**
     * Consumes spaces
     * 
     * @param input
     *            the input stream
     * @throws NullPointerException
     *             see {@link NullPointerException}
     * @throws IOException
     *             see {@link IOException}
     */
    protected void consumeSpaces(final MpsInputStream input)
	    throws NullPointerException, IOException {
	int in = input.read();
	while (in == 32) {
	    in = input.read();
	}
	input.unread(in);
    }

    /**
     * Consumes a given number of spaces
     * 
     * @param input
     *            the inputStream
     * @param numberOfSpaces
     *            the number of spaces to consume
     * @return
     * @throws IOException
     *             see {@link IOException}
     */
    protected boolean consumeSpaces(final PushbackInputStream input,
	    final int numberOfSpaces) throws IOException {
	int i = 0;
	while (i < numberOfSpaces) {
	    if (input.read() != 32) {
		return true;
	    }
	    i++;
	}
	return false;
    }

}
